/**
 * 详情对话框
 */
var ActivityInfoDlg = {
    data: {
        type: "",
        content: "",
        goodsId: "",
        title: "",
        adminId: "",
        rewardWay: "",
        applyWay: "",
        maxAttendCount: "",
        maxApplicantsCount: "",
        alreadyApplicationsCount: "",
        alreadyApplicationsList: "",
        finalAttendCount: "",
        finalAttendList: "",
        submissList: "",
        rewardList: "",
        swParticipationWay: "",
        swRewardList: "",
        swReport: "",
        swTestList: "",
        praise: "",
        watch: "",
        status: "",
        beginApplyTime: "",
        endApplyTime: "",
        beginSubmissionTime: "",
        endSubmissionTime: "",
        activityEndTime: "",
        deptid: "",
        img:"",
        createTime: "",
        updateTime: "",
        recordStatus: ""
    }
    ,editor:null
};

layui.use(['form', 'admin', 'ax','simTool','laydate'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var simTool = layui.simTool;
    var laydate = layui.laydate;

    //让当前iframe弹层高度适应
    //admin.iframeAuto();
    var tool = new simTool("/upload","#picBox1");
    tool.uploadImg();
    //初始化编辑器
    var E = window.wangEditor;
    var editor = new E('#description_toolbar', '#description_text');
    // 将图片大小限制为 2M
    editor.customConfig.uploadImgMaxSize = 200 * 1024 * 1024;
    editor.customConfig.uploadImgServer =Feng.ctxPath+'/upload/wang'; //'common/batch/imgUpdate';
    editor.customConfig.uploadFileName = 'file' ;
    editor.customConfig.customAlert = function (info){
        // info 是需要提示的内容
        alert('上传失败，请重试：' + info);
    }
    editor.create();
    ActivityInfoDlg.editor = editor;

    /**
     * 时间顺序校验
     * @param val
     * @returns {boolean}
     */
    ActivityInfoDlg.timeVerify=function(val){
        if(val.length<2){
            return true;
        }
        for(var i=0;i<val.length;i++){
            if(i+1>=val.length){
                return true;
            }
            if(val[i]==""||val[i+1]==""){
                return false;
            }
            var cha = (Date.parse(val[i]) - Date.parse(val[i+1])) / 86400000 * 24;
            if(cha<0){
                continue;
            }else {
                return false;
            }
        }
        return true;
    }

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        console.log("img"+$("#picBox1").find("#img").attr("src"))
        var b = ActivityInfoDlg.timeVerify([$("#beginSubmissionTime").val(),
            $("#endSubmissionTime").val(),$("#beginApplyTime").val(),$("#endApplyTime").val(),$("#activityEndTime").val()
        ]);
        if(!b){
            Feng.error("请正确填写活动时间，注意顺序");
            return false;
        }
        var ajax = new $ax(Feng.ctxPath + "/activity/addItem", function (data) {
            Feng.success("添加成功！");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set("content",ActivityInfoDlg.editor.txt.html());
        ajax.set("img",$("#picBox1").find("#img").attr("src"));
        ajax.set(data.field);

        ajax.start();
    });

    laydate.render({
        elem: '#beginApplyTime',
        type: 'datetime',
    });
    laydate.render({
        elem: '#endApplyTime',
        type: 'datetime',
    });
    laydate.render({
        elem: '#beginSubmissionTime',
        type: 'datetime',
    });
    laydate.render({
        elem: '#endSubmissionTime',
        type: 'datetime',
    });
    laydate.render({
        elem: '#activityEndTime',
        type: 'datetime',

    });
});